// JavaScript Document
Object.extendss = function(parent,child){
	for(var i in parent){
		if(typeof parent[i] != 'object'){
			child[i] = parent[i];	
		}	
		else if(parent[i].constructor == Array){
			child[i] = 	Object.clone(parent[i]);
		}
		else{
			child[i] = child[i] || {};
			Object.extendss(parent[i],child[i]);
		}
	}
};

var page = page || {};
//ajax信息获取后通过json数组方式传递到该扩展对象进行数据展示
//该扩展只提供数据展示，css在单独的css文件中进行默认设置
(function(page){
	function show(data,option){
		//最上层的node(展示样板)
		var target = page.view.defaults.container.children[0];
		target.style.display = "";
		var frag = document.createDocumentFragment();
		var dlen = data.length;
		if(dlen ==0){
			var span = create("span");
			span.innerHTML = "--无数据--";
			target.style.display = "none";
			page.view.defaults.container.appendChild(span);
			return;
		}
		for(var i =0;i<dlen;i++){
			var current = target.cloneNode(true);
			//解析全部的node并均衡放置
			var contas = {};
			parser(current,contas);
			
			var values = data[i];
			for(var j in values){
				if(option[j]){
					var id = option[j]["param"].id;
					if(option[j]['bind']){
						bindValue(values,option[j]['bind'],contas[id]);
					}
					newInstanceNode(values[j],option[j],contas[id]);	
				}
			}	
			frag.appendChild(current);			
		}
		target.style.display = "none";
		page.view.defaults.container.appendChild(frag);
	}
	
	//额外绑定值
	function bindValue(values,bind,node){
		for(var i in bind){
			var param = bind[i];
			node[i] = values[param]||param;
		}
	}
	
	function newInstanceNode(value,option,node){
		var reloadPicture = page.view.defaults.reloadPicture;
		var styl = option["style"]||{};
		var param = option['param'];
		//绑定属性	
		for(var i in param){
			node[i] = param[i];	
		}
		//值绑定
		if(node.tagName == "IMG"&&value!=null){
			if(reloadPicture){
				node.image = value;
				var path = value.substring(0,value.lastIndexOf("."))
				if(value.indexOf(".jpg")>0){
					node.src = path+".png";
				}
				else{
					node.src = path+".jpg";
				}
				node.onload = loaded;
				//node.src = value;
			}
			else{
				node.src = value;
			}
		}
		else if(node.tagName=="INPUT"&&node.type=="checkbox"){
			if(value == "true"){
				node.checked = "checked";	
			}	
		}
		else if(node.value){
			node.value = value;	
		}
		else{
			node.innerHTML = value;	
		}
		//绑定样式
		var s = node.style;
		for(var i in styl){
			s[i] = styl[i];
		}
	}
	function loaded(event){
		event = event||window.event;
		var target = event.target;
		target.src = target.image;
		target.onload = null;
	}
	function parser(target,contas){
		var children = target.children;
		var len = children.length;
		for(var i =0;i<len;i++){
			var child = children[i];
			var id = child.id;			
			if(child.children&&child.children.length>0){
				parser(child,contas);
			}
			contas[id] = child;
		}
	}
	function get(id){
		return document.getElementById(id);
	}
	function create(tagName){
		return document.createElement(tagName);	
	}
	//检查调用view方法传入的参数是否符合格式
	//如果参数正确返回true,否则返回false
	function checkParam(data,option){		
		if(data == null || data == "undefined"){
			return false;	
		}
		else if(data.length ==0){
			return true;	
		}
		//检查option参数项是否与data数据项对象
		if(option == null || option == "undefined"){
			return false;	
		}
		/*else{	
			var d = data[0];
			for(var i in d){
				if(option[i] == null || option[i] == 'undefined'){
					alert("option数据项与data数据项不匹配");
					return false;	
				}	
			}
		}*/
		return true;
	}
	var clear = function(cid,hasHeader){
		var container = get(cid);
		var one = container.children[0].cloneNode(true);
		var two = null;
		if(hasHeader){
			two = container.children[1].cloneNode(true);
		}
		container.innerHTML = "";
		container.appendChild(one);
		if(two){
			container.appendChild(two);
		}
	}
	/*
	view:展示的container必须为界面已存在的节点，且id必须为规定格式
	id格式为：page_view
		
	参数事例
	var data = [
			{'title':"xunxun",'gist':12,'publishTime':"2013-12-12 23:1:12",'replyTimes':23},	
			{'title':"xunxun",'gist':12,'publishTime':"2013-12-12 23:1:12",'replyTimes':23},	
			{'title':"xunxun",'gist':12,'publishTime':"2013-12-12 23:1:12",'replyTimes':23}
		]
	var option = {
			'title':{
				'style':{'display':'block'},
				'param':{'id':"title"},
				'bind':{'userId':'userId','title':'userName'}
			},
			'gist':{
				'param':{'id':"gist"}			
			},
			'publishTime':{
				'style':{},
				'param':{'id':"publishTime"}		
			},
			'replyTimes':{
				'style':{},
				'param':{'id':"replyTimes"}		
			}			
		}			
	data为通过ajax获取到的json数据数组 【必须】
	option的数据项必须参照data中json数据项编写，
		其中json的name必须相同且不能少 【必须】
		
	调用方式：page.view('show',data,option);其中'show'为页面的一个node的id，即父容器
*/
	page.view = function(cid,data,option,reload){
		page.view.defaults.container = get(cid);
		if(page.view.defaults.container == null||page.view.defaults.container.children.length==0){
			alert("请先指定展示数据的容器和基础标签");
			return;
		}
		if(!checkParam(data,option)){
			alert("参数有误！请仔细检查");
			return;
		}
		page.view.defaults.reloadPicture = reload||false;
		show(data,option);
	}
	page.get = get;
	page.create = create;
	page.view.clear = clear;
	page.view.methods = {
	}
	page.view.parseOptions = function(target){
		
	}
	page.view.defaults = {
		container:null,
		reloadPicture:false
	}
		  
})(page);

//封装ajax获取数据对象
(function(page){
	var xmlHttp=null;
	var createXMLHttpRequest=function(){
		try{
			if(window.ActiveObject){
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");	
			}	
			else if(window.XMLHttpRequest){
				xmlHttp = new XMLHttpRequest();	
			}
		}	
		catch(e){
			alert(e.name + ":" +e.message);	
		}
	};
	var _doGet=function(url,parameter){
		try{
			var rand = Math.round(Math.random()*10000);
			url = url + "?random="+rand;
			if(parameter != null &&parameter!={}){
				url = url + "&"+parameter;				
			}
			xmlHttp.open("GET",url,false);
			xmlHttp.send(null);
		}
		catch(e){
			alert(e.name + ":" +e.message);	
		}
	};
	var _doPost=function(url,parameter){
		try{
			xmlHttp.open("POST",url,false);
			xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			xmlHttp.send(parameter);
		}
		catch(e){
			alert(e.name + ":" +e.message);	
		}
	};
	/*
		参数说明：url目标地址，parameter为需要传递的参数，且格式已经
		格式化，method=post/get，callBack回调函数
	*/
	var ajaxLoad=function(url,parameter,method,callBack){
		createXMLHttpRequest();
		if(method.toLowerCase()=="post"){
			_doPost(url,parameter);
		}
		else{
			_doGet(url,parameter);	
		}
		xmlHttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
				//var value = self.xmlHttp.responseText;
				var result = xmlhttp.responseText.split('#');
				callBack(result);	
			}
		};
		xmlHttp.onreadystatechange=null;
	}		
	
	
	
	page.ajax={
		data:ajaxLoad
	}
})(page);







